package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDish(dishDTO);
        return Result.success();

    }

    /**
     * 菜品分页查询
     *
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> getPage(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.getPage(dishPageQueryDTO);
        return Result.success(pageResult);

    }

    /**
     * 删除菜品 要删除就得先查询状态
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 修改回显
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);

    }
    /**
     *修改菜品信息
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result putDish(@RequestBody DishDTO dishDTO){
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();


    }

    /**
     * 根据分类id查询对应的菜品信息
     */
    @GetMapping("/list")
    public Result getByCategoryId(Long categoryId) {
        System.out.println(categoryId);
        List<Dish> dishList = dishService.getByCategoryId(categoryId);
        System.out.println(dishList);
        return Result.success(dishList);
    }

    /**
     * 根据指定的key样式，删除对应数据
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }




}